//
//  MKWelcomeController.swift
//  Weibo
//
//  Created by mac on 15/12/16.
//  Copyright © 2015年 MK. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class MKWelcomeController: UIViewController {
    
    //设置view的背景图片 - 在加载视图前 也就是在刚加载视图时 让view ＝ UIImageView 这叫偷梁换柱
    override func loadView() {
        view = backgroundImage
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 添加子视图 并用框架设置frame
        setUpSubViews()
        
        //FIXME: - 动画一定不要再viewDidLoad中写 因为此时view的frame还没有被确定下来 此方法结束后view才有确定的frame, 所以要到viewDidAppear这个方法中去执行动画
        //print("viewDidLoad- 测试view是否有frame\(view)")
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //print("viewDidAppear- 测试view是否有frame\(view)")
        
        //添加动画
        setViewAnimation()
    }
    
    //添加子视图 并设置frame
    private func setUpSubViews() {
        view.addSubview(iconImage)
        view.addSubview(titleLabel)
        
        iconImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            //设置尺寸
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        titleLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_centerX)
            make.top.equalTo(iconImage.snp_bottom).offset(22)
        }
        
        iconImage.sd_setImageWithURL(MKUserInfoViewModer().iconURL, placeholderImage: UIImage(named: "avatar_default_big"))
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
        //iconImage.layer.borderWidth = 4
        //iconImage.layer.borderColor = UIColor.lightGrayColor().CGColor
        
    }
    
    //设置欢迎页面的动画
    private func setViewAnimation() {
        
        titleLabel.alpha = 0.0
        let offset = -UIScreen.mainScreen().bounds.height + 180
        
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            //如果存在此约束 就更新约束 / 如果不存在此约束 就创建此约束
            self.iconImage.snp_updateConstraints { (make) -> Void in
                make.bottom.equalTo(self.view.snp_bottom).offset(offset)
            }
            /*FIXME: -自动布局实现动画时需要注意
            
            通过snp_updateConstraints更改约束 用self.view.layoutIfNeeded()收集到所有约束的更改 然后在本次循环结束之前进行强制刷新页面
            */
            self.view.layoutIfNeeded()

            }) { (_) -> Void in
                
                //print("欢迎页面 头像动画完成后的回调")
                
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.titleLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                        //print("欢迎页面 标签动画完成后的回调")
                        //FIXME: -利用通知 更改根控制器跳转到到首页
                        NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: nil)
                })
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    
    //懒加载子视图
    lazy var backgroundImage: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    lazy var iconImage: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    lazy var titleLabel: UILabel = UILabel(tText: (MKUserInfoViewModer().userName ?? ""), tColor: UIColor.lightGrayColor(), tFont: 14)

}
